package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtValidationCode;
import com.ymttest.utils.EnvSetup;

/**
 * 获取手机验证码
 * 
 * @author wanghuiyong
 *
 */
public class YmtValidationCodeWapper {
	SqlManager manage;

	public YmtValidationCodeWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * 获取对应手机号的最新一条验证码
	 * 
	 * @param phoneNum
	 *            手机号
	 * @return 最新一条验证码,如果没有返回null
	 * @throws InterruptedException
	 */
	public String getValidateCode(String phoneNum) throws InterruptedException {
		Thread.sleep(1000);
		String vcode = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("sActionUser", phoneNum);
		List<YmtValidationCode> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtValidationCodeMapper.selectNewestCode", phoneNum);
		if (userList.size() > 0) {
			vcode = userList.get(0).getsValidateCode();
		}
		// manage.close();
		return vcode;
	}
	
	/**
	 * 获取对应手机号的最新一条验证码
	 * 
	 * @param phoneNum
	 *            手机号
	 * @return 最新一条验证码,如果没有返回null
	 * @throws InterruptedException
	 */
	public YmtValidationCode getValidateData(String phoneNum) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("sActionUser", phoneNum);
		List<YmtValidationCode> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtValidationCodeMapper.selectNewestCode", phoneNum);
		if (userList.size() > 0) {
			 return userList.get(0);
		}
		return null;
	}
	
	public List<YmtValidationCode> getByActionUserAndValidateType(String actionUser, Integer validateType) {
		Map map = new HashMap();
		map.put("sActionUser", actionUser);
		map.put("iValidateType", validateType);
		List<YmtValidationCode> result = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtValidationCodeMapper.getByActionUserAndValidateType", map);
		manage.commit();
		return result;
	}
	
	public void setiValidateStatus(String sValidateCode, int iValidateStatus) {
		Map map = new HashMap();
		map.put("sValidateCode", sValidateCode);
		map.put("iValidateStatus", iValidateStatus);
		manage.update(
				"com.ymttest.database.mapping.YmtValidationCodeMapper.setiValidateStatus", map);
		manage.commit();
	}
	
	public void setExpiredTime(String sValidateCode, Date expiredTime) {
		Map map = new HashMap();
		map.put("sValidateCode", sValidateCode);
		map.put("expiredTime", expiredTime);
		manage.update(
				"com.ymttest.database.mapping.YmtValidationCodeMapper.setExpiredTime", map);
		manage.commit();
	}
	
	public static void main(String[] args) throws InterruptedException {
		YmtValidationCodeWapper test = new YmtValidationCodeWapper();
		String vcode = test.getValidateCode("13665163415@emailactive.com");
		Logger.debug(vcode);
	}
}
